package com.jin.Logic;

import com.jin.Exception.http.ParameterException;
import com.jin.bo.SkuOrderBO;
import com.jin.dto.OrderDTO;
import com.jin.dto.SkuInfoDTO;
import com.jin.model.OrderSku;
import com.jin.model.Sku;
import lombok.Getter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

//@Service
//@Scope("prototype")
public class OrderChecker {
    private OrderDTO orderDTO;
    private List<Sku> serverSkuList;
    private CouponChecker couponChecker;
    private Integer maxSkuLimit;
    @Getter
    private List<OrderSku> orderSkuList = new ArrayList<>();

    public OrderChecker(OrderDTO orderDTO, List<Sku> serverSkuList, CouponChecker couponChecker,Integer maxSkuLimit) {
        this.orderDTO = orderDTO;
        this.serverSkuList = serverSkuList;
        this.couponChecker = couponChecker;
        this.maxSkuLimit = maxSkuLimit;
    }

    public String getLeaderImg() {
        return  this.serverSkuList.get(0).getImg();
    }

    public String getLeaderTitle() {
        return this.serverSkuList.get(0).getTitle();
    }

    public Integer getTotalCount() {
        return this.orderDTO.getSkuInfoList()
                .stream()
                .map(SkuInfoDTO::getCount)
                .reduce(Integer::sum)
                .orElse(0);
    }

    public void isOK(){
        //总原价校验
        //对商品是否下架进行校验
        //售罄商品
        //购买商品数量超出库存
        //超出购买商品数量
        //优惠券校验
        BigDecimal serverTotalPrice = new BigDecimal("0");
        List<SkuOrderBO> skuOrderBOList = new ArrayList<>();
        skuNotOnSale(orderDTO.getSkuInfoList().size(),serverSkuList.size());
        for (int i = 0; i < serverSkuList.size(); i++) {
            Sku sku = serverSkuList.get(i);
            SkuInfoDTO skuInfoDTO = orderDTO.getSkuInfoList().get(i);
            containsSoldOutSku(sku);
            beyondSkuStock(sku,skuInfoDTO);
            beyondMaxSkuLimit(skuInfoDTO);
            serverTotalPrice.add(calculateSkuOrderPrice(sku,skuInfoDTO));
            skuOrderBOList.add(new SkuOrderBO(sku,skuInfoDTO));
            orderSkuList.add(new OrderSku(sku,skuInfoDTO));
        }
        totalPriceIsOk(orderDTO.getTotalPrice(), serverTotalPrice);
        //优惠券校验
        if (this.couponChecker != null) {
            this.couponChecker.isOk();
            this.couponChecker.canBeUsed(skuOrderBOList, serverTotalPrice);
            this.couponChecker.finalTotalPriceIsOk(orderDTO.getFinalTotalPrice(), serverTotalPrice);
        }
    }

    private void totalPriceIsOk(BigDecimal orderTotalPrice, BigDecimal serverTotalPrice) {
        if (orderTotalPrice.compareTo(serverTotalPrice) != 0) {
            throw new ParameterException(50005);
        }
    }

    private BigDecimal calculateSkuOrderPrice(Sku sku,SkuInfoDTO skuInfoDTO){
         if (skuInfoDTO.getCount()<=0){
             throw new ParameterException(50007);
         }
         return sku.getActualPrice().multiply(new BigDecimal(skuInfoDTO.getCount()));
    }
    private void skuNotOnSale(int count1,int count2){
        if (count1 != count2){
            throw new ParameterException(50002);
        }
    }

    private void containsSoldOutSku(Sku sku){
        if (sku.getStock()==0){
            throw new ParameterException(50001);
        }
    }

    //超卖的情况
    private void beyondSkuStock(Sku sku,SkuInfoDTO skuInfoDTO){
        if (sku.getStock()<skuInfoDTO.getCount()){
            throw new ParameterException(50003);
        }
    }

    //超出最大购买限制
    private void beyondMaxSkuLimit(SkuInfoDTO skuInfoDTO){
        if (skuInfoDTO.getCount()>maxSkuLimit){
            throw new ParameterException(50004);
        }
    }
}
